package com.kuizii.base.common.utils;


import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * @Author: nuanyang
 * @Date: 2019/7/3 0003 11:40
 */
public class EncryptHelper {
    private static final String AES_KEY_STRING = "designByU0k0eTnY";//16位字符

    /**
     * AES加密字符串 兼容.net收银台,固定加密模式为CBC，填充方式PKCS5Padding,兼容.NET收银台中PKCS7padding,并且设置偏移量
     *
     * @param content 需要被加密的字符串
     * @return 密文
     */
    public static byte[] aesEncrypt(String content) {
        try {
            byte[] byteContent = content.getBytes(StandardCharsets.UTF_8);
            byte[] byteKey = AES_KEY_STRING.getBytes(StandardCharsets.UTF_8);

            SecretKeySpec key = new SecretKeySpec(byteKey, "AES");// 转换为AES专用密钥

            IvParameterSpec iv = new IvParameterSpec(byteKey);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

            cipher.init(Cipher.ENCRYPT_MODE, key, iv);// 初始化为加密模式的密码器

            byte[] result = cipher.doFinal(byteContent);// 加密

            return result;

        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String aesEncryptToBase64String(String content) {
        byte[] data = aesEncrypt(content);
        return new String(Base64.getEncoder().encode(data));
    }


    public static String aesDecryptFromBase64Str(String encryptBase64Str) {
        return aesDecrypt(Base64.getDecoder().decode(encryptBase64Str));
    }

    /**
     * 解密
     *
     * @param result 加密后的密文byte数组
     * @param
     */
    public static String aesDecrypt(byte[] result) {

        Cipher cipher;
        try {
            byte[] byteKey = AES_KEY_STRING.getBytes(StandardCharsets.UTF_8);

            SecretKeySpec key = new SecretKeySpec(byteKey, "AES");// 转换为AES专用密钥

            IvParameterSpec iv = new IvParameterSpec(byteKey);
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

            cipher.init(Cipher.DECRYPT_MODE, key, iv);// 初始化为加密模式的密码器

            byte[] decryptData = cipher.doFinal(result);// 加密

            return new String(decryptData, StandardCharsets.UTF_8);

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }

        System.out.println("jdk aes desrypt:" + new String(result));
        return new String(result);
    }
}
